//角色管理模块接口
import request from '@/utils/request'
import type { RoleData, RoleResponseData } from './type'
enum API {
  //获取全部职位接口
  ALLROLE_URL = '/admin/acl/role',
  //新增职位
  ADDROLE_URL = '/admin/acl/role/save',
  //更新职位
  UPDATEROLE_URL = '/admin/acl/role/update',
  //获取树形控件全部的菜单与按钮的数据
  ALLPERMISSTION_URL = '/admin/acl/permission/toAssign/',
  //给相应职位分配权限
  SETPERMISSION_URL = '/admin/acl/permission/doAssign',
  //删除某个职位
  DELETEROLE_URL = 'admin/acl/role/delete',
}

//获取全部角色，roleName参数可有可无，如果收集到了rolename，则根据rolename来获取角色信息，可用于搜索发送请求
export const reqAllRoleList = (
  page: number,
  limit: number,
  roleName: string,
) => {
  request.get<any, RoleResponseData>(
    API.ALLROLE_URL + `${page}/${limit}/?roleName=${roleName}`,
  )
}
//添加和更新职位合并为一个接口方法
export const reqAddOrUpdateRole = (data: RoleData) => {
  //根据有无id判断发送更新还是添加请求
  if (data.id) {
    return request.put<any, any>(API.UPDATEROLE_URL, data)
  } else {
    return request.put<any, any>(API.ADDROLE_URL, data)
  }
}
//获取全部菜单与按钮的接口方法
export const reqAllMenuList = (roleId: number) => {
  request.get(API.ALLPERMISSTION_URL + roleId)
}
//分配权限
export const reqSetPermission = (roleId: number, permissionId: number[]) => {
  request.post(
    API.SETPERMISSION_URL + `roleId=${roleId}&permissionId=${permissionId}`,
  )
}
//删除职位
export const reqRemoveRole = (roleId: number) => {
  request.delete(API.DELETEROLE_URL + roleId)
}
